home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / MAP Viewer / WADLoader.cpp < prev    next >
C/C++ Source or Header  |  2003-10-09  |  6KB  |  240 lines

  1. /*
  2. Half-Life MAP viewing utility.
  3. Copyright (C) 2003  Ryan Samuel Gregg
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include "stdafx.h"
  21. #include "WADLoader.h"
  22.  
  23. bool CWADLoader::MapFile(LPCTSTR szFileName, LPVOID *pView, LPDWORD pdwFileSize)
  24. {
  25.     HANDLE hFile = NULL;
  26.     HANDLE hFileMapping = NULL;
  27.     LPVOID lpView = NULL;
  28.     DWORD dwFileSize = 0;
  29.  
  30.     hFile = CreateFile( 
  31.         szFileName,                    // Name of file
  32.         GENERIC_READ,                // Desired access
  33.         FILE_SHARE_READ,            // Share mode
  34.         NULL,                        // Security attributes
  35.         OPEN_EXISTING,                // Creation disposition
  36.         FILE_FLAG_SEQUENTIAL_SCAN,    // Attributes and flags
  37.         NULL);                        // Template file    
  38.  
  39.     if(hFile == INVALID_HANDLE_VALUE)
  40.         return false;
  41.  
  42.     // Store this away for now...
  43.     dwFileSize = GetFileSize(hFile, NULL);
  44.  
  45.     if(dwFileSize == -1)
  46.         return false;
  47.     
  48.     hFileMapping = CreateFileMapping( 
  49.         hFile,                        // Handle to file
  50.         NULL,                        // Security attributes
  51.         PAGE_READONLY,                // Protection
  52.         0,                            // Max size high
  53.         0,                            // Max size low
  54.         NULL);                        // Name of mapping object    
  55.     
  56.     if(hFileMapping == NULL)
  57.         return false;
  58.  
  59.     // We don't need this anymore
  60.     CloseHandle(hFile);
  61.  
  62.     // Map to the entire file
  63.     lpView = MapViewOfFile(
  64.         hFileMapping,                // Handle to the mapping
  65.         FILE_MAP_READ,                // Desired access
  66.         0,                            // Offset high
  67.         0,                            // Offset low
  68.         0);                            // Number of bytes
  69.  
  70.     if(lpView == NULL)
  71.         return false;
  72.  
  73.     // We don't need this anymore
  74.     CloseHandle(hFileMapping);
  75.  
  76.     if(pView)
  77.     {
  78.         *pView = lpView;
  79.     }
  80.     
  81.     if(pdwFileSize)
  82.     {
  83.         *pdwFileSize = dwFileSize;
  84.     }
  85.  
  86.     return true;
  87. }
  88.  
  89. bool CWADLoader::LoadWADFile(String *sFile, ArrayList *Textures, int *iTexturesLoaded)
  90. {
  91.     LPVOID            lpView = NULL;
  92.     LPWAD3_HEADER    lpHeader = NULL;
  93.     LPWAD3_LUMP        lpLump = NULL;
  94.     LPWAD3_MIP        lpMip = NULL;
  95.  
  96.     DWORD            dwNumLumps = 0;
  97.     DWORD            dwTableOffset = 0;
  98.     DWORD            dwFileSize = 0;
  99.     DWORD            dwFilePos = 0;
  100.     DWORD            dwPaletteOffset = 0;
  101.     WORD            wPaletteSize = 0;
  102.     String            *sTextureName;
  103.     DWORD            dwWidth = 0;
  104.     DWORD            dwHeight = 0;
  105.     LPBYTE            lpImageData;
  106.     LPBYTE            lpPalette;
  107.     LPBYTE            lpImage;
  108.  
  109.     unsigned int    iID;
  110.     bool            bLoadTex;
  111.     Texture            *Tex;
  112.     unsigned char cTextureName __gc[] = new unsigned char __gc[16];
  113.     LPCTSTR            szFileName = NULL;
  114.  
  115.     *iTexturesLoaded = 0;
  116.     
  117.     System::Text::ASCIIEncoding    *ASCII = new System::Text::ASCIIEncoding();
  118.  
  119.     szFileName = (LPCTSTR)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(sFile).ToPointer());
  120.  
  121.     if(MapFile(szFileName, &lpView, &dwFileSize) == false)
  122.     {
  123.         System::Runtime::InteropServices::Marshal::FreeHGlobal((IntPtr((void*)szFileName)));
  124.         return false;
  125.     }
  126.  
  127.     System::Runtime::InteropServices::Marshal::FreeHGlobal((IntPtr((void*)szFileName)));
  128.  
  129.     if(dwFileSize < sizeof(WAD3_HEADER))
  130.         return false;
  131.  
  132.     lpHeader = (LPWAD3_HEADER)lpView;
  133.  
  134.     if(lpHeader->identification != WAD3_ID)
  135.         return false;
  136.  
  137.     dwNumLumps = lpHeader->numlumps;
  138.     dwTableOffset = lpHeader->infotableofs;
  139.  
  140.     if(((dwNumLumps * sizeof(WAD3_LUMP)) + dwTableOffset) > dwFileSize)
  141.         return false;
  142.  
  143.     lpLump = (LPWAD3_LUMP)((LPBYTE)lpView + dwTableOffset);
  144.  
  145.     for(DWORD i = 0; i < dwNumLumps; i++, lpLump++)
  146.     {        
  147.         if(lpLump->type != WAD3_TYPE_MIP)
  148.             continue;
  149.  
  150.         // Find out where the MIP actually is.
  151.         dwFilePos = lpLump->filepos;
  152.  
  153.         if(dwFilePos >= dwFileSize)
  154.             return false;
  155.  
  156.         lpMip = (LPWAD3_MIP)((LPBYTE)lpView + dwFilePos);
  157.  
  158.         for(DWORD j = 0; j < 16; j++)
  159.         {
  160.             /*if(lpMip->name[j] == '\0')
  161.             {
  162.                 for(;j < 16; j++)
  163.                 {
  164.                     cTextureName[j] = '\0';
  165.                 }
  166.             }
  167.             else
  168.             {
  169.                 cTextureName[j] = (unsigned char)lpMip->name[j];
  170.             }*/
  171.             cTextureName[j] = (unsigned char)lpMip->name[j];
  172.         }
  173.  
  174.         bLoadTex = false;
  175.         sTextureName = ASCII->GetString(cTextureName, 0, strlen(lpMip->name))->ToLower();
  176.  
  177.         for(INT j = 0; j < Textures->Count; j++)
  178.         {
  179.             Tex = static_cast<Texture*>(Textures->get_Item(j));
  180.  
  181.             if(Tex->Loaded)
  182.                 continue;
  183.  
  184.             if(String::Compare(sTextureName, Tex->TextureName) == 0)
  185.             {
  186.                 bLoadTex = true;
  187.                 break;
  188.             }
  189.         }
  190.  
  191.         if(!bLoadTex)
  192.             continue;
  193.  
  194.         dwWidth = lpMip->width;
  195.         dwHeight = lpMip->height;
  196.  
  197.         dwPaletteOffset = GET_MIP_DATA_SIZE(dwWidth, dwHeight);
  198.         wPaletteSize = *(WORD *)((LPBYTE)lpMip + dwPaletteOffset);
  199.         dwPaletteOffset += sizeof(WORD);
  200.  
  201.         lpPalette = ((LPBYTE)lpMip + dwPaletteOffset);
  202.  
  203.         if((dwFilePos + lpMip->offsets[0] + dwWidth * dwHeight) > dwFileSize)
  204.             return false;
  205.  
  206.         lpImageData = ((LPBYTE)lpMip + lpMip->offsets[0]);
  207.  
  208.         lpImage = NULL;
  209.         lpImage = new BYTE[dwWidth * dwHeight * 3];
  210.  
  211.         if(lpImage == NULL)
  212.             return false;
  213.  
  214.         for(DWORD j = 0, k = 0; j < dwWidth * dwHeight; j++)
  215.         {
  216.             lpImage[k++] = lpPalette[lpImageData[j] * 3];
  217.             lpImage[k++] = lpPalette[lpImageData[j] * 3 + 1];
  218.             lpImage[k++] = lpPalette[lpImageData[j] * 3 + 2];
  219.         }
  220.  
  221.         glGenTextures(1, &iID);
  222.  
  223.         glBindTexture(GL_TEXTURE_2D, iID);
  224.  
  225.         gluBuild2DMipmaps(GL_TEXTURE_2D, 3, dwWidth, dwHeight, GL_RGB, GL_UNSIGNED_BYTE, lpImage);
  226.  
  227.         Tex->Loaded = true;
  228.         Tex->ID = iID;
  229.         Tex->Width = dwWidth;
  230.         Tex->Height = dwHeight;
  231.  
  232.         (*iTexturesLoaded)++;
  233.  
  234.         delete[] lpImage;
  235.     }
  236.  
  237.     UnmapViewOfFile(lpView);
  238.  
  239.     return true;
  240. }